﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace TrackingAssist.WebTrends
{
    public class ResourceManager
    {
        private static ImageList iconList;

        private const string ICON_PASS = "Pass.bmp";
        private const string ICON_DYNAMIC = "Dynamic.bmp";
        private const string ICON_FAIL = "Fail.bmp";
        private const string ICON_APP = "WebTrends.bmp";
        private const string ICON_WARNING = "Warning.bmp";
        private const string ICON_UnKnown = "UnKnown.bmp";
        private const string IMAGE_DIR = "ImageList";
        private const string ICON_PROCESS = "loading5.gif";

        public static ImageList GetIconList()
        {
            if (iconList != null)
                return iconList;

            iconList = new ImageList();

            Assembly assembly = Assembly.GetExecutingAssembly();
            var assemblyName = assembly.GetName().Name;
            Stream streamAtlasIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_APP));
            Bitmap bitmapAtlasIcon = new Bitmap(streamAtlasIcon);
            iconList.Images.Add(AppIcons.AppIcon.ToString(), bitmapAtlasIcon);

            Stream streamPassIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_PASS));
            Bitmap bitmapPassIcon = new Bitmap(streamPassIcon);
            iconList.Images.Add(AppIcons.Pass.ToString(), bitmapPassIcon);

            Stream streamFailIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_FAIL));
            Bitmap bitmapFailIcon = new Bitmap(streamFailIcon);
            iconList.Images.Add(AppIcons.Fail.ToString(), bitmapFailIcon);

            //Stream streamDynamicIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_DYNAMIC));
            //Bitmap bitmapDynamicIcon = new Bitmap(streamDynamicIcon);
            //iconList.Images.Add(AppIcons.Dynamic.ToString(), bitmapDynamicIcon);

            Stream streamWarnIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_WARNING));
            Bitmap bitmapWarnIcon = new Bitmap(streamWarnIcon);
            iconList.Images.Add(AppIcons.Miss.ToString(), bitmapWarnIcon);

            Stream streamUnKnownIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_UnKnown));
            Bitmap bitmapUnKnownIcon = new Bitmap(streamUnKnownIcon);
            iconList.Images.Add(AppIcons.UnKnown.ToString(), bitmapUnKnownIcon);

            Stream streamProcessIcon = assembly.GetManifestResourceStream(string.Concat(assemblyName, ".", IMAGE_DIR, ".", ICON_PROCESS));
            Bitmap bitmapProcessIcon = new Bitmap(streamProcessIcon);
            iconList.Images.Add(AppIcons.Process.ToString(), bitmapProcessIcon);
            
            return iconList;
        }

        public static Image GetImage(int index)
        {
            if (iconList == null || index >= iconList.Images.Count)
                return null;

            return iconList.Images[index];
        }

        public static Image GetImage(AppIcons icon)
        {
            if (iconList == null)
                return null;

            return iconList.Images[(int) icon];
        }
    }
}
